package ph.edu.upd.eee.upcatarchivesearch;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import java.util.Random;

/**
 * Created by smartwire on 10/9/2014.
 */
public class AboutSurfaceView extends SurfaceView implements SurfaceHolder.Callback {
    public AboutSurfaceView(Context c) {
        super(c);
    }

    @Override
    public void surfaceCreated(SurfaceHolder surfaceHolder) {
        // Do nothing...
    }

    @Override
    public void surfaceChanged(SurfaceHolder surfaceHolder, int fmt, int w, int h) {
        updateSurface(surfaceHolder, w, h);
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
        // Do nothing...
    }

    private void updateSurface(SurfaceHolder surfaceHolder, int w, int h) {
        int randNum;
        Random randomGen;
        Canvas canvas;

        canvas = surfaceHolder.lockCanvas();
        randomGen = new Random();

        try {
            synchronized (getHolder()) {
                Paint paint;
                Path paths[];
                Resources resources;

                HexTiles hexTiles;

                resources = getResources();
                paint = new Paint(Paint.ANTI_ALIAS_FLAG);

                canvas.drawColor(resources.getColor(R.color.maroon));

                hexTiles = new HexTiles(15);
                paths = hexTiles.createTiledHexs(w, h);

                // Fill hexagon tiles
                paint.setStyle(Paint.Style.FILL);
                for(Path path : paths) {
                    // Randomize colors; 95% Maroon, 5% Green
                    randNum = randomGen.nextInt(100);
                    if(randNum < 95) {
                        paint.setColor(resources.getColor(R.color.maroon));
                    }
                    else {
                        paint.setColor(resources.getColor(R.color.forestgreen));
                    }

                    canvas.drawPath(path, paint);
                }

                // Draw hexagon tile strokes
                paint.setStyle(Paint.Style.STROKE);
                paint.setColor(Color.BLACK);
                paint.setStrokeWidth(0.5f);
                for(Path path : paths) {
                    canvas.drawPath(path, paint);
                }
            }
        }
        finally {
            if(canvas != null) {
                surfaceHolder.unlockCanvasAndPost(canvas);
            }
        }
    }
}
